<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:question:add']"
        >我要提问</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="questionList" @selection-change="handleSelectionChange">
      <el-table-column label="问题id" align="center" prop="questionId" />
      <el-table-column label="问题标题" align="center" prop="questionTitle" />
      <el-table-column label="问题简介" align="center" prop="questionResume" />
      <el-table-column label="创建人" align="center" prop="questionUser" />
      <el-table-column label="创建时间" align="center" prop="questionCreatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.questionCreatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="喜欢人数" align="center" prop="questionFavorcount" />
      <el-table-column label="收藏量" align="center" prop="questionCollectcount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                      v-hasPermi="['system:question:edit']"
                    >修改</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                      v-hasPermi="['system:question:remove']"
                    >删除</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="getDetail(scope.row)"
          >详情</el-button>
          <!--            @click="handleUpdate(scope.row)"
                      v-hasPermi="['system:question:edit']"-->
          <!-- 详情 -->
          <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAnswer"
              v-hasPermi="['system:answer:add']"
            >我要回答</el-button>
            <el-dialog :title="title" :visible.sync="openanswer" width="500px" append-to-body>
              <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="回答内容" prop="questionResume">
                  <el-input v-model="form.answerContent" placeholder="请输入你的回答" />
                </el-form-item>

              </el-form>

              <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitAnswer">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
              </div>
            </el-dialog>
            <el-table v-loading="loading" :data="answerListByQuestionId" @selection-change="handleSelectionChange">
              <el-table-column label="回答人id" align="center" prop="answerUser" />
              <el-table-column label="回答内容" align="center" prop="answerContent" />
              <el-table-column label="回答时间" align="center" prop="answerTime" width="180">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.answerTime, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
            </el-table>
          </el-dialog>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="openadd" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问题标题" prop="questionTitle">
          <el-input v-model="form.questionTitle" placeholder="请输入问题标题" />
        </el-form-item>
        <el-form-item label="问题简介" prop="questionResume">
          <el-input v-model="form.questionResume" placeholder="请输入问题简介" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



  </div>
</template>

<script>
import {
  getQuestion,
  addQuestion,
  updateQuestion,
} from "@/api/qasystem/question";
import { getQuestionAnswer,addAnswerByQuestionId } from "@/api/qasystem/answer";
import { getQuestionByLabelId } from "@/api/qasystem/labelquestion";


export default {
  name: "Question",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      quId: 0,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问题表格数据
      questionList: [],
      answerListByQuestionId:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openadd:false,
      openanswer:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        questionTitle: null,
        questionResume: null,
        questionUser: null,
        questionCreatetime: null,
        questionFavorcount: null,
        questionCollectcount: null,
        questionIsdel: null
      },
      queryAnswerParams: {
        pageNum: 1,
        pageSize: 10,
        answerQuestionId: null,
        answerUser: null,
        answerContent: null,
        answerTime: null,
        answerIsdel: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDetail();
  },
  methods: {
    /** 查询问题列表 */
    getList() {
      this.loading = true;
      getQuestionByLabelId(this.queryParams,this.$route.query.labelId).then(response => {
        this.questionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openadd = false;
      this.openanswer = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        questionId: null,
        questionTitle: null,
        questionResume: null,
        questionUser: null,
        questionCreatetime: null,
        questionFavorcount: null,
        questionCollectcount: null,
        questionIsdel: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.questionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openadd = true;
      this.title = "添加问题";
    },
    handleAnswer(){
      this.openanswer = true;
      this.title = "添加回答";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const questionId = row.questionId || this.ids
      getQuestion(questionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改问题";
      });
    },
    /** 详情按钮 */
    getDetail(row){

      const questionId = row.questionId;
      this.quId = questionId;
      getQuestionAnswer(this.queryAnswerParams,questionId).then(response => {
        //this.form = response.data;

        this.answerListByQuestionId = response.rows;
        this.total = response.total;
        this.open = true;
        this.title = "它的回答";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.questionId != null) {
            updateQuestion(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQuestion(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitAnswer(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.form.answerContent,this.quId);
          addAnswerByQuestionId(this.form.answerContent,this.quId).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.openanswer = false;
            this.getList();
          });

        }
      });
    }

  }
};
</script>
